﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Common;
using Basic.Model.Entity;
using Basic.Model.EntityManager;

namespace Basic.Business
{
    /// <summary>
    /// 字典模块，只允许添加，不允许修改删除
    /// </summary>
    public class DictionaryTypeAdmin
    {
        /// <summary>
        /// 添加一个字典
        /// </summary>
        /// <param name="dictionaryTypeName"></param>
        public static void AddDictionaryType(string dictionaryTypeName)
        {
            ISession session = null;
            ITransaction trans = null;
            DictionaryType dictionaryType = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                if (DictionaryTypeManager.GetByCondition(session, dictionaryTypeName, false) != null)
                    throw new Exception("当前系统已存在这个字典，请换一个字典名。");
                dictionaryType = new DictionaryType();
                dictionaryType.dictionaryTypeName = dictionaryTypeName;
                DictionaryTypeManager.Add(session, dictionaryType);

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }

        /// <summary>
        /// 根据字典名称获取一个下拉列表HTML，供用户选取字典选项
        /// </summary>
        /// <param name="dictionaryTypeName"></param>
        /// <param name="selectId"></param>
        /// <param name="selectedId"></param>
        /// <returns></returns>
        public static string GetDictionarySelectHtmlString(string dictionaryTypeName, string selectId, int selectedId)
        {
            ISession session = null;
            ITransaction trans = null;
            DictionaryType dictionaryType = null;
            StringBuilder html = new StringBuilder();
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                dictionaryType = DictionaryTypeManager.GetByCondition(session, dictionaryTypeName, true);
                if (dictionaryType == null)
                    throw new Exception("这个字典不存在。");
                trans.Commit();

                html.AppendFormat("<select id=\"{0}\" name=\"{0}\" class=\"input\">", selectId);
                IEnumerator<Dictionary> dictionary = dictionaryType.dictionarys.GetEnumerator();
                while (dictionary.MoveNext())
                {
                    if (dictionary.Current.dictionaryId != selectedId)
                        html.AppendFormat("<option value=\"{0}\">{1}</option>", dictionary.Current.dictionaryId.ToString(), dictionary.Current.dictionaryValue);
                    else
                        html.AppendFormat("<option value=\"{0}\" selected=\"selected\">{1}</option>", dictionary.Current.dictionaryId.ToString(), dictionary.Current.dictionaryValue);
                }
                html.Append("</select>");
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                html.Append("读取字典失败");
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            return html.ToString();
        }
    }
}
